package com.baiyue.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baiyue.entity.CommodityConsumption;
import com.baiyue.exception.GlobalException;
import com.baiyue.mapper.CommodityConsumptionMapper;
import com.baiyue.model.commodityConsumption.CommodityConsumptionDataRequest;
import com.baiyue.model.commodityConsumption.CommodityConsumptionDataResponse;
import com.baiyue.result.CodeMsg;
import com.baiyue.service.CommodityConsumptionService;
import com.baiyue.utils.BeanMapperUtil;
import com.baiyue.utils.UserHolderUtil;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 *
 * @author 伍波
 * @date 2024-06-20
 */
@Service
public class CommodityConsumptionServiceImpl implements CommodityConsumptionService {
    private static final Logger logger = LoggerFactory.getLogger(ProductCategoryServiceImpl.class);
    @Autowired
    private CommodityConsumptionMapper commodityConsumptionMapper;

    /**
     * 新增商品消费信息
     * @param requestList
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void insertList(List<CommodityConsumptionDataRequest> requestList){
        logger.info("插入商品消费记录入参:{}", JSONObject.toJSONString(requestList));
        List<CommodityConsumption> commodityConsumptionList = BeanMapperUtil.mapList(
                requestList,CommodityConsumption.class
        );
        String user = UserHolderUtil.getUser().getUsername();
        for (CommodityConsumption commodityConsumption : commodityConsumptionList) {
            if (StringUtils.isEmpty(commodityConsumption.getCommodityName())) {
                logger.warn("插入商品消费失败,商品名为空：{}", JSONObject.toJSONString(commodityConsumption));
                throw new GlobalException(CodeMsg.BIND_ERROR.fillArgs("商品名称"));
            }
            commodityConsumption.setCreator(user);
            commodityConsumption.setUpdater(user);
            commodityConsumption.setCreateTime(new Date());
            commodityConsumption.setUpdateTime(new Date());
            commodityConsumptionMapper.insert(commodityConsumption);
        }
    }

    /**
     * 根据uuid查询商品消费详情
     * @param request
     * @return
     */
    @Override
    public List<CommodityConsumptionDataResponse> findByUuid(CommodityConsumptionDataRequest request){
        logger.info("根据uuid查询商品消费信息入参:{}", JSONObject.toJSONString(request));
        if (StringUtils.isEmpty(request.getUuid())){
            logger.warn("根据uuid 查询消费信息失败：uuid为空");
            throw new GlobalException(CodeMsg.BIND_ERROR.fillArgs("uuid"));
        }
        List<CommodityConsumption> result = commodityConsumptionMapper.findByUuid(request.getUuid());
        if(result.isEmpty()){
            return new ArrayList<>();
        }
        List<CommodityConsumptionDataResponse> responses = BeanMapperUtil.mapList(
                result, CommodityConsumptionDataResponse.class
        );
        return responses;
    }


    /**
     * 根据ID删除商品消费信息
     * 此方法接收一个CommodityConsumptionDataRequest对象作为参数，从中提取ID信息，
     * 并利用ID来删除商品消费信息如果请求的ID为空，则抛出异常
     *
     * @param request 包含待删除商品消费信息ID的请求对象
     * @throws GlobalException 如果请求ID为空，则抛出此异常
     */
    @Override
    public void deleteById(CommodityConsumptionDataRequest request){
        // 记录删除操作的入参信息
        logger.info("根据id删除商品消费信息入参：{}", JSONObject.toJSONString(request));

        // 检查请求中的ID是否为空，如果为空，则记录警告信息并抛出异常
        if (request.getId() == null){
            logger.warn("根据id删除商品消费信息失败：id为空");
            throw new GlobalException(CodeMsg.BIND_ERROR.fillArgs("id"));
        }

        // 调用Mapper层方法，根据ID删除商品消费信息
        commodityConsumptionMapper.deleteById(request.getId());
    }
}
